﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectProjectActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            var sid = this.AsyncDialog("Id", d =>
            {
                this.Prompt("请输入Id");
                return this.DialogValue("none");
            });
            var cmdId = UMC.Data.Utility.UInt64Parse(sid) ?? 0;

            var project = cmdId > 0 ? (DataFactory.Instance().Project(cmdId) ?? new Project()) : new Project();


            if (project.Id.HasValue == false)
            {
                if (sid == "New")
                {
                    this.Context.Send(new UIClick(request.Model, "ProjectNew"), true);
                }

                if (user.IsAuthenticated == false)
                {
                    response.Redirect(new WebMeta().Put("type", "login"));
                }

                var userId = user.Id;

                var attent = DataFactory.Instance().Attention(userId.Value) ?? new Attention() { Id = userId.Value };
                var dUser = DataFactory.Instance().Attention(userId.Value) ?? new Attention();

                var form = (request.SendValues ?? new UMC.Web.WebMeta()).GetDictionary();
                var webr2 = UMC.Web.WebResource.Instance();
                response.Redirect(new WebMeta().Put("IsMaster", request.IsMaster).Put("Look", attent.SubjectViewTotal ?? 0).Put("Reply", attent.SubjectReplyTotal ?? 0).Put("Count", attent.SubjectTotal ?? 0)
                    .Put("Src", webr2.ImageResolve(request.Url, userId.Value, "1", 4)).Put("Alias", user.Alias).Put("Signature", dUser.Signature ?? "未有签名").Put("ProjectCount", attent.ProjectTotal ?? 0));

            }
            var webr = UMC.Web.WebResource.Instance();
            var hash = new Hashtable();
            hash["Id"] = project.Id;
            hash["Reply"] = project.ReplyTotal ?? 0;
            hash["Look"] = project.ViewTotal ?? 0;
            hash["Count"] = project.SubjectTotal ?? 0;
            hash["MemberCount"] = project.MemberTotal ?? 0;
            hash["Keywords"] = project.Keywords;
            hash["Desc"] = (project.Description ?? "未填写").Trim();
            hash["IsCreator"] = project.user_id == user.Id;
            if (user.IsAuthenticated)
            {
                var pm = DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);// != null;
                if (pm != null)
                {
                    switch (pm.AuthType)
                    {
                        case WebAuthType.Admin:
                            hash["IsAuth"] = true;
                            goto case WebAuthType.User;
                        case WebAuthType.User:
                            if ((project.PublishTime ?? 0) + 6000 < UMC.Data.Utility.TimeSpan())
                            {
                                hash["releaseId"] = project.Id;
                            }
                            break;
                    }
                }
                var idReader = DataFactory.Instance().ProjectReader(this.Context.Token.UserId.Value, project.Id.Value) != null;
                if (idReader)
                {
                    hash["FollowText"] = "项目已订阅";//: "关注项目";

                }
                else
                {
                    hash["FollowText"] = pm != null ? "已加入项目" : "关注项目";
                }
            }
            else
            {
                hash["FollowText"] = "关注项目";

            }
            hash["Code"] = project.Code;


            hash["Src"] = webr.ImageResolve(request.Url, project.Id.Value, "1", 4, UMC.Data.Utility.TimeSpan(project.ModifiedTime ?? DateTime.Now).ToString());
            hash["Name"] = project.Caption;

            response.Redirect(hash);

        }

    }
}